<template>
  <div class="app-container">
      管理员姓名：  <el-input
    style="width: 200px"
    placeholder="请输入内容"
    prefix-icon="el-icon-search"
    v-model="adminName">
  </el-input>
  管理员账号：  <el-input
    style="width: 200px"
    placeholder="请输入内容"
    prefix-icon="el-icon-search"
    v-model="adminAccount">
  </el-input>
  管辖区域：  <el-select v-model="areaId2"
                        clearable
                     placeholder="请选择区">
            <el-option v-for="item in regionList"
                       :key="item.areaId"
                       :label="item.areaName"
                       :value="item.areaId">
            </el-option>
          </el-select>
  <el-row style="margin:10px">注册时间：   <el-date-picker v-model="begin"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择日期">
              </el-date-picker>
  -           <el-date-picker v-model="end"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择日期">
              </el-date-picker>
    <el-button type="primary" style="margin: 20px" @click="selectAdmin()">搜索</el-button>
    <el-button type="primary" style="margin: 20px" @click="resetForm()">重置</el-button>
    <el-button type="primary" @click="dialogFormVisible = true">添加</el-button>
    </el-row>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
      type="selection"
      width="55">
      </el-table-column>
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column label="管辖区域" width=auto>
        <template slot-scope="scope">
          {{ scope.row.areaList[0].areaName }}
        </template>
      </el-table-column>
      <el-table-column label="管理员姓名" width=auto align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系方式"
                       width="110"
                       align="center">
        <template slot-scope="scope">
          {{ scope.row.userPhone }}
        </template>
      </el-table-column>
      <el-table-column label="管理员账号" width=auto align="center">
        <template slot-scope="scope">
          {{ scope.row.account }}
        </template>
      </el-table-column>
      <el-table-column label="密码" width=auto align="center">
        <template slot-scope="scope">
          {{ scope.row.userPassword }}
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="注册时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ new Date(scope.row.registerDate).toLocaleDateString() }}</span>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" width=auto align="center">
        <template slot-scope="scope">
          <el-button type="success" plain @click="editor(scope.row)">编辑</el-button>
          <el-button type="danger" plain @click="deleteAdmin(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加区审核员"
               :visible.sync="dialogFormVisible">
      <el-form :model="regionCheck">
        <el-form-item label="管理员姓名"
                      :label-width="formLabelWidth">
          <el-input v-model="regionCheck.userName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管辖区"
                      :label-width="formLabelWidth">
          <el-select v-model="areaId"
                     placeholder="请选择区">
            <el-option v-for="item in regionList"
                       :key="item.areaId"
                       :label="item.areaName"
                       :value="item.areaId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式"
                      :label-width="formLabelWidth">
          <el-input v-model="regionCheck.userPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员账号"
                      :label-width="formLabelWidth">
          <el-input v-model="regionCheck.account"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      :label-width="formLabelWidth">
          <el-input v-model="regionCheck.userPassword"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addRegionCheck()">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改区审核员信息"
               :visible.sync="dialogForm1Visible">
      <el-form :model="regionCheck1">
        <el-form-item label="管理员姓名"
                      :label-width="form1LabelWidth">
          <el-input v-model="regionCheck1.userName"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管辖区"
                      :label-width="form1LabelWidth">
          <el-select v-model="areaId1"
                     placeholder="请选择区">
            <el-option v-for="(item,index) in regionList"
                       :key="index"
                       :label="item.areaName"
                       :value="item.areaId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系方式"
                      :label-width="form1LabelWidth">
          <el-input v-model="regionCheck1.userPhone"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="管理员账号"
                      :label-width="form1LabelWidth">
          <el-input v-model="regionCheck1.account"
                    autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码"
                      :label-width="form1LabelWidth">
          <el-input v-model="regionCheck1.userPassword"
                    autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="dialogForm1Visible = false">取 消</el-button>
        <el-button type="primary"
                   @click="updateRegionCheck()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分页插件 -->
    <div class="block"   style="float:right">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="pageIndex"
      :page-sizes="pageSizes"
      :page-size.sync="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="Total">
    </el-pagination>
    </div>
  </div>
</template>

<script>
import { getRegionCheck, addRegionCheck, updateRegionCheck, deleteAdmin } from '@/api/regionAdmin/regionCheck'
import { getRegionList } from '@/api/regionAdmin/region'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      regionList: [],
      listLoading: true,
      adminName: null,
      adminAccount: null,
      areaId: "",
      areaId1: "",
      areaId2: null,
      begin: null,
      end: null,
      //分页
      currentPage:1,
      Total:0,
      pageIndex:1,
      pageSize:5,
      needPage:true,
      pageSizes: [5, 10, 15, 20],
      areaIdList:"",
      newName:"",
      dialogFormVisible: false,
      dialogForm1Visible: false,
      regionCheck: {
        // userName: '',
        // areaId: '',
        // userPhone: '',
        // account: '',
        // userPassword: ''
        userId: "",
        userName: "",
        account: "",
        userPassword: "",
        userPhone: "",
        registerDate: "",
        roleId: '1',
        areaList: [],
        shelters: null,
        role: null
      },
      regionCheck1: {
        userId: "",
        userName: "",
        account: "",
        userPassword: "",
        userPhone: "",
        registerDate: "",
        roleId: '1',
        areaList: [],
        shelters: null,
        role: null
      },
      formLabelWidth: '120px',
      form1LabelWidth: '120px'
    }
  },
  created() {
    this.fetchData(),
    this.getRegionList()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      //获取区管理员信息
      getRegionCheck(1, this.pageIndex, this.pageSize).then(response => {
        // console.log(response)
        this.list = response.obj.list
        localStorage.setItem("list",JSON.stringify(this.list))
        this.Total = response.obj.total
        this.listLoading = false
        // console.log(this.list)
        // console.log(this.Total)
      })
      // 获取区信息
      // getRegionList(0).then(response => {
      //   this.regionList = response.list
      //   // console.log(this.regionList)
      // })
    },
    //获取区信息
    getRegionList(){
      this.pageIndex = null
      this.pageSize = null
      this.needPage = false
       getRegionList(0,this.pageIndex,this.pageSize,this.needPage).then(response => {
        this.regionList = response.obj.list
        // console.log(this.regionList)
      })
    },
    //查询管理员
    selectAdmin(){
      if(this.adminName == "" && this.adminAccount == "" && this.areaId2 == "" && this.begin == "" && this.end == ""){
        alert("查询条件不能为空！")
        getRegionCheck(1).then(response =>{
        this.list = response.obj.list
        this.listLoading = false
      })
      }
      getRegionCheck(1,this.pageIndex,this.pageSize,this.adminName,this.adminAccount,this.areaId2,this.begin,this.end).then(response =>{
        this.list = response.obj.list
        this.Total = response.obj.total
        this.listLoading = false
      })
      // if(this.adminName){
      //   getRegionCheck(1,this.adminName).then(response =>{
      //     this.list = response.obj.list
      //   })
      // }
      // if(this.adminAccount){
      //   getRegionCheck(1,this.adminAccount).then(response =>{
      //     this.list = response.obj.list
      //   })
      // }
      // if(this.areaId2){
      //  getRegionCheck(1,this.areaId2).then(response =>{
      //     this.list = response.obj.list
      //   })
      //   this.listLoading = false
      // }
    },
    //重置查询条件
     resetForm(){
      this.adminName = null
      this.adminAccount = null
      this.areaId2 = null
      this.begin = null
      this.end = null
      this.fetchData()
      // this.list = JSON.parse(localStorage.getItem("list"))
      // this.Total = this.list.length
    },
    //增加管理员
      addRegionCheck(){
        addRegionCheck(this.regionCheck,this.areaId).then(response => {
          let message = response.msg
          this.$message({
              type: 'success',
              message: message
          })
          this.dialogFormVisible = false
          this.fetchData()      
          this.regionCheck.userName = "",
          this.regionCheck.account = "",
          this.regionCheck.userPhone = "",
          this.regionCheck.userPassword = "",
          this.areaId = ""
      })
      },
    //编辑管理员
      editor(item){
        this.dialogForm1Visible = true
        this.areaId1 = item.areaList[0].areaName
        this.regionCheck1 = item
        // console.log(this.regionList)
        // console.log(this.regionCheck1)
        // console.log(item.areaList[0].areaId)
        // console.log(item)
        // console.log(1)
        // console.log(this.areaId1)
        // this.dialogForm1Visible = true
        // this.areaIdList = this.areaId1
        // console.log(this.areaIdList = item.areaList[0].areaId)
        // console.log(this.areaIdList)
        // console.log(typeof(this.areaIdList))
      },
      updateRegionCheck(){
        this.areaIdList = this.areaId1
        updateRegionCheck(this.regionCheck1,this.areaIdList).then(response =>{
          // console.log(this.areaIdList)
          let message = response.msg
           this.$message({
              type: 'success',
              message: message
              })
        this.dialogForm1Visible = false
        this.fetchData()
        })
      },
      //删除管理员
      deleteAdmin(item){
        console.log(item)
        deleteAdmin(item.userId).then(response =>{
          this.$message({
            message:response.msg,
            type:'success'
          })
          this.fetchData()
        })
      },
      // 分页
         // 每页显示的条数
        handleSizeChange(val) {
            // 改变每页显示的条数 
            this.pageSize=val
            // this.pageSize=val
            // 点击每页显示的条数时，显示第一页
            // this.fetchData.getRegionCheck(1,val,1)
            // 在改变每页显示的条数时，要将页码显示到第一页
            // this.pageIndex=1
            this.fetchData()
        },
          // 显示第几页
        handleCurrentChange(val) {
            // 改变默认的页数
            this.pageIndex=val
            // 切换页码时，要获取每页显示的条数
            // this.pageSize=val
            // this.pageSize = (val)*(this.pageSize)
            this.fetchData()
        },
  }
}
</script>
